package com.tajiang.business.businesscount.wallet;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.tajiang.business.R;
import com.tajiang.business.common.activity.BaseActivity;
import com.tajiang.business.common.utils.CashUtils;
import com.tajiang.business.common.utils.PostDataTools;
import com.tajiang.business.common.utils.Res;
import com.tajiang.business.model.AccountWithDrawInfo;

import org.json.JSONArray;
import org.json.JSONObject;

import butterknife.BindView;

/**
 * 提现进度
 */
public class WithDrawProgressActivity extends BaseActivity {

    @BindView(R.id.withdraw_progress_time)
    TextView tvTime1;

    @BindView(R.id.withdraw_progress_time2)
    TextView tvTime2;

    @BindView(R.id.withdraw_progress_time3)
    TextView tvTime3;

    @BindView(R.id.withdraw_progress_state)
    TextView tvState;

    @BindView(R.id.withdraw_progress_fee)
    TextView tvFee;

    @BindView(R.id.withdraw_progress_content)
    TextView tvContent;

    @BindView(R.id.withdraw_progress_icon_1)
    ImageView imgHandle;

    @BindView(R.id.withdraw_progress_icon_2)
    ImageView imgSuccess;

    @BindView(R.id.withdraw_progress_state_1)
    TextView tvState1;

    @BindView(R.id.withdraw_progress_state_2)
    TextView tvState2;

    private AccountWithDrawInfo accountWithDrawInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initTopBar() {
        setTitle("提现进度");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_with_draw_progress);
    }

    @Override
    protected void initData() {

        accountWithDrawInfo = WithdrawRecordMenuActivity.mAccountWithDrawInfo;

        tvTime1.setText(accountWithDrawInfo.getCreateTime());
        tvFee.setText(CashUtils.changeF2Y(accountWithDrawInfo.getBalance(), -1));

        String strContent = String.format("提现到 %s (%s) %s", accountWithDrawInfo.getBankName(), accountWithDrawInfo.getAccountNo(), accountWithDrawInfo.getAccountOwner());
        tvContent.setText(strContent);

        PostDataTools.acct_withdrawstate(this, accountWithDrawInfo.getExchangeId(), new PostDataTools.DataCallback() {

            @Override
            public void callback(boolean flag, String message, Object object) {

                JSONArray jsonArray = ((JSONObject) object).optJSONArray("data");
                if(jsonArray == null){
                    return;
                }

                tvTime2.setText("");
                tvTime3.setText("");
                for(int i=0; i<jsonArray.length(); i++){

                    JSONObject jsonObject = jsonArray.optJSONObject(i);
                    int state = jsonObject.optInt("state");
                    String time = jsonObject.optString("createTime");

                    //0：提交申请 1： 处理中 2：到账 3:拒绝
                    if(state == 0){
                        tvState.setText("提交申请");
                        tvTime2.setText(time);
                        tvState1.setTextColor(Res.getColor(R.color.text_black));
                        imgHandle.setBackgroundResource(R.drawable.mine_withdraw_success);
                    }else if(state == 1){
                        tvState1.setTextColor(Res.getColor(R.color.text_black));
                        imgHandle.setBackgroundResource(R.drawable.mine_withdraw_success);
                        tvState.setText("处理中");
                    }else if(state == 2){
                        tvState2.setTextColor(Res.getColor(R.color.text_black));
                        imgHandle.setBackgroundResource(R.drawable.mine_withdraw_success);
                        imgSuccess.setBackgroundResource(R.drawable.mine_withdraw_success);
                        tvState.setText("到账");
                        tvTime3.setText(time);
                    }else if(state == 3){
                        tvState.setText("提现失败");
                        tvState2.setText("提现失败");
                        tvTime3.setText(time);
                        tvState.setTextColor(Res.getColor(R.color.red));
                        tvState2.setTextColor(Res.getColor(R.color.red));
                        imgSuccess.setBackgroundResource(R.drawable.icon_withdraw_failure);
                    }
                }
            }
        });
    }

}
